package youquan;


import com.alibaba.fastjson.JSONObject;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

import java.io.IOException;
import java.util.List;

/**
 * author: zhangxiong
 * created on: 17-2-15 下午9:28
 * description:
 */
public class YouQuanHttpManager {


    //请求连接超时时间
    public static final long CONNECT_TIMEOUT = 10000L;
    //请求读取超时时间
    public static final long READ_TIMEOUT = 10000L;
    //实例
    private static YouQuanHttpManager mInstance;
    //OkHttpClient对象
    private static OkHttpClient mOKHttpClient;

    private HttpCallBack httpCallBack;

    private static final int RESULT_ERROR = -1;
    private static final int RESULT_SUCESS = 0;

    public YouQuanHttpManager(OkHttpClient okHttpClient) {
        if (okHttpClient == null) {
            mOKHttpClient = new OkHttpClient();
        } else {
            mOKHttpClient = okHttpClient;
        }
    }

    public static YouQuanHttpManager init(OkHttpClient okHttpClient) {
        if (mInstance == null) {
            synchronized (YouQuanHttpManager.class) {
                if (mInstance == null) {
                    mInstance = new YouQuanHttpManager(okHttpClient);
                }
            }
        }
        return mInstance;
    }

    public static YouQuanHttpManager getmInstance() {
        return mInstance;
    }



    public String get(String url, Param header) {
        Request request = new Request.Builder()
                .url(url)
                .header(header.getKey(), header.getValue())
                .get()
                .build();
        try {
            return mOKHttpClient.newCall(request).execute().body().string();
        } catch (IOException e) {
            return null;
        }
    }

    public String post(String url, List<Param> params) {
        FormBody.Builder bodyBulder = new FormBody.Builder();
        for (Param param : params) {
            bodyBulder.add(param.getKey(), param.getValue());
        }
        RequestBody requestBody = bodyBulder.build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        try {
            return mOKHttpClient.newCall(request).execute().body().string();
        } catch (IOException e) {
            return null;
        }
        //startRequest(request);
    }

    public String post(String url, List<Param> params, Param headers, HttpCallBack httpCallBack) {
        this.httpCallBack = httpCallBack;
        FormBody.Builder bodyBulder = new FormBody.Builder();
        for (Param param : params) {
            bodyBulder.add(param.getKey(), param.getValue());
        }
        RequestBody requestBody = bodyBulder.build();
        Request request = new Request.Builder()
                .header(headers.getKey(), headers.getValue())
                .url(url)
                .post(requestBody)
                .build();
        try {
            return mOKHttpClient.newCall(request).execute().body().string();
        } catch (IOException e) {
            return null;
        }
    }


    public interface HttpCallBack {

        void onResponse(JSONObject jsonObject);

        void onFailure(String errorMsg);
    }


}
